package com.db.serialize;

import com.db.serialize.domain.SensorData;
import com.db.serialize.network.AbstractNetwork;
import com.db.serialize.serializer.SensorDataSerializer;
import com.db.serialize.serializer.SensorDataSerializerFactory;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class SerializeClient extends AbstractNetwork {
    public static void main(String[] args) {
        System.out.println("client started");

        SensorDataSerializer serializer = SensorDataSerializerFactory.getSerializer();

        try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT);
             ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {

            System.out.println("connection started");

            long startTime = System.currentTimeMillis();
            for (int i = 0; i < ITERATION_COUNT; i++) {
                SensorData sensorData = SensorData.generate();
                Object data = serializer.serialize(sensorData);
                objectOutputStream.writeObject(data);
            }

            String message = (String) objectInputStream.readObject();
            System.out.println(message);

            long time = System.currentTimeMillis() - startTime;
            System.out.println("success");

            System.out.println(time + "ms");

        } catch (IOException e) {
            System.out.println("input output exception");
        } catch (ClassNotFoundException e) {
            System.out.println("class not found exception");
        }
    }
}
